package gat.common.config;

import com.tencentcloudapi.common.CommonClient;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Optional;

@Data
@Configuration
@ConfigurationProperties(prefix = "tencent")
public class TencentConfig {

    private List<TencentAccount> accounts;

    @Data
    public static class TencentAccount {
        private String id;
        private String accountId;
        private String secretId;
        private String secretKey;
        private String endpoint;
        private String region;
        private String service;
        private String version;
    }

    public CommonClient createClient(String accountId) {
        Optional<TencentAccount> accountOpt = getAccounts().stream()
                .filter(account -> account.getAccountId().equals(accountId))
                .findFirst();

        TencentAccount account = accountOpt.orElseThrow(
                () -> new IllegalArgumentException("未找到账号ID: " + accountId)
        );

        // 1. 实例化认证对象
        Credential cred = new Credential(account.getSecretId(), account.getSecretKey());

        // 2. 配置HTTP选项
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(account.getEndpoint());

        // 3. 实例化客户端配置
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        // 4. 实例化要请求产品的client对象
        return new CommonClient(
                account.getService(),
                account.getVersion(),
                cred,
                account.getRegion(),
                clientProfile
        );
    }

    public static void main(String[] args) {
        // 1. 实例化认证对象
        Credential cred = new Credential("IKIDTyNfs610vgGdAas22sY6czHPM8qNiomY", "d3JQNMNuNg1FI8mHdsyvhInCQXjpmbSC");

        // 2. 配置HTTP选项
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("intlpartnersmgt.intl.tencentcloudapi.com");

        // 3. 实例化客户端配置
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        // 4. 实例化要请求产品的client对象
        CommonClient client = new CommonClient(
                "intlpartnersmgt",
                "2022-09-28",
                cred,
                "ap-singapore",
                clientProfile
        );


        try {
            String params1 = "{\"CustomerUin\":[200040659152]}";
            String resp1 = client.call("DescribeCustomerInfo", params1);
            System.out.println(resp1);

          /*  String params2 = "{\"ClientUin\":200040629615,\"Type\":0}";

            String resp2 = client.call("ForceQN", params2);
            System.out.println(resp2);

            String params3 = "{\"CustomerUin\":[200040629615]}";
            String resp3 = client.call("DescribeCustomerInfo", params3);
            System.out.println(resp3);*/
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
    }

}